const formatMessage = require('format-message');
const ArgumentType = require('../../extension-support/argument-type');
const BlockType = require('../../extension-support/block-type');

/**
 * Url of icon to be displayed at the left edge of each extension block.
 * @type {string}
 */
// eslint-disable-next-line max-len
const iconURI = '';


/**
 * Url of icon to be displayed in the toolbox menu for the extension category.
 * @type {string}
 */
// eslint-disable-next-line max-len
const menuIconURI = '';

/**
 * Class for the speech blocks.
 * @constructor
 */
class Scratch3HteAiBlocks {
    constructor (runtime) {
        /**
         * The runtime instantiating this block package.
         * @type {Runtime}
         */
        this.runtime = runtime;
        this.text = '';
        this._resetEdgeTriggerUtterance = this._resetEdgeTriggerUtterance.bind(this);
        this.runtime.on('PROJECT_START', this._resetEdgeTriggerUtterance.bind(this));
        this.text2 = '';
        window.electron.start();
    }


    _resetEdgeTriggerUtterance () {
        this.text = '';
    }


    getSounds () {
        const s = this.runtime._editingTarget.sprite.sounds;
        const data = [];
        for (const v in s) {
            data.push({
                text: s[v].name,
                value: s[v].name
            });
        }
        return data;
    }

    langs () {
        return [
            {
                value: '1536',
                text: formatMessage({
                    id: 'hte.ext.speech.mandarin',
                    default: '普通话(支持简单的英文识别)',
                    description: 'speech mandarin'
                })
            },
            {
                value: '1537',
                text: formatMessage({
                    id: 'hte.ext.speech.chinese',
                    default: '普通话(纯中文识别)',
                    description: 'speech chinese'
                })
            },
            {
                value: '1737',
                text: formatMessage({
                    id: 'hte.ext.speech.english',
                    default: '英语',
                    description: 'speech english'
                })
            },
            {
                value: '1637',
                text: formatMessage({
                    id: 'hte.ext.speech.cantonese',
                    default: '粤语',
                    description: 'speech cantonese'
                })
            },
            {
                value: '1837',
                text: formatMessage({
                    id: 'hte.ext.speech.sichuanDialect',
                    default: '四川话',
                    description: 'speech sichuanDialect'
                })
            },
            {
                value: '1936',
                text: formatMessage({
                    id: 'hte.ext.speech.field',
                    default: '普通话远场',
                    description: 'speech field'
                })
            }
        ];
    }

    getInfo () {

        return {
            id: 'hteai',
            name: formatMessage({
                id: 'hte.ext.speech.name',
                default: '语音转文字',
                description: 'speech name'
            }),
            blockIconURI: iconURI,
            menuIconURI: menuIconURI,
            blocks: [
                // {
                //   opcode: 'speakformbaidu',
                //   text: '选择一个录音 [RECORD] 选择语言[languages]',
                //   arguments: {
                //     RECORD: {
                //       type: ArgumentType.STRING,
                //       defaultValue: '喵',
                //       menu: 'getSound',
                //     },
                //     languages: {
                //       type: ArgumentType.STRING,
                //       defaultValue: '1536',
                //       menu: 'languages',
                //     }

                //   }
                // },
                // {
                //   opcode: 'getspeak',
                //   text: '语音输出(录音)',
                //   blockType: BlockType.REPORTER
                // },
                {
                    opcode: 'getnaturalspeak',
                    text: formatMessage({
                        id: 'hte.ext.speech.speech',
                        default: '语言输入 [languages]',
                        description: 'speech speech'
                    }),
                    blockType: BlockType.COMMAND,
                    arguments: {
                        languages: {
                            type: ArgumentType.STRING,
                            defaultValue: '1536',
                            menu: 'languages'
                        }
                    }
                },
                {
                    opcode: 'getspeaktime',
                    text: formatMessage({
                        id: 'hte.ext.speech.output',
                        default: '语音输出',
                        description: 'speech output'
                    }),
                    blockType: BlockType.REPORTER

                }
            ],
            menus: {
                getSound: 'getSounds',
                languages: this.langs()
            }
        };
    }

    speakformbaidu (arg, r) {
        const sound = r.thread.target.sprite.sounds.find(v => v.name == arg.RECORD);
        if (!sound) return;
        let devpid = arg.languages;
        const l = this.langs();
        if (l.find(v => v.value != arg.languages)) {
            devpid = '1536';
        }
        const wav = new Blob([sound.asset.data], sound.asset.assetType);
        const f = new FormData();
        f.append('wav', wav, 'a.wav');
        f.append('devpid', devpid);
        axios.post('https://public.hterobot.com/security/baidu/sound', f, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }).then(res => {
            console.log(res);
            if (res.data.err_no == 0) {
                this.text = res.data.result[0];
            }
        })
            .catch(err => {
                this.text = '';
                window.electron.message(err);
            });
    }

    getspeak () {
        return this.text;
    }

    getnaturalspeak (arg) {
        const x = window.electron.htebaidu(arg.languages);
        return x.then(res => {
            console.log(res);
            if (res.data.err_no == 0) {
                this.text2 = res.data.result[0];
            } else {
                this.text = '';
            }
        }).catch(err => {
            this.text = '';
            window.electron.message(err);
        });
    }

    getspeaktime () {
        return this.text2;
    }
}
module.exports = Scratch3HteAiBlocks;
